package com.smartrodin.springbootrabbitmq.config;

import com.smartrodin.springbootrabbitmq.util.Constant;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author: arnold_nie
 * @date: 2020/6/12 17:07
 * FileName: FanoutRabbitConfig
 * Description: 发布/订阅(类似广播)模式队列配置
 */
@Configuration
public class FanoutRabbitConfig {

    /**
     * 交换机与队列绑定的RoutingKey
     */
    private static final String ROUTING_KEY_INTEGRAL = "routingKey.integral";
    private static final String ROUTING_KEY_COUPON = "routingKey.coupon";

    /**
     * 创建推送数据队列交换机
     *
     * @return the exchange
     */
    @Bean(Constant.PUSH_DATA_FANOUT_EXCHANGE)
    public Exchange pushDataFanoutExchange() {
        return ExchangeBuilder.fanoutExchange( Constant.PUSH_DATA_FANOUT_EXCHANGE ).durable( true ).build();
    }

    /**
     * 推送用户注册赠送积分队列
     *
     * @return
     */
    @Bean(Constant.QUEUE_PUSH_INTEGRAL)
    public Queue queuePushIntegral() {
        return new Queue( Constant.QUEUE_PUSH_INTEGRAL );
    }

    /**
     * 推送用户注册赠送优惠券队列
     *
     * @return
     */
    @Bean(Constant.QUEUE_PUSH_COUPON)
    public Queue queuePushCoupon() {
        return new Queue( Constant.QUEUE_PUSH_COUPON );
    }

    /**
     * 用户注册赠送积分队列和交换机绑定关系，并且指定 RoutingKey
     */
    @Bean
    public Binding integralBindingFanout(@Qualifier(Constant.QUEUE_PUSH_INTEGRAL) Queue queue,
                                         @Qualifier(Constant.PUSH_DATA_FANOUT_EXCHANGE) FanoutExchange exchange) {
        return BindingBuilder.bind( queue ).to( exchange );
    }

    /**
     * 用户注册赠送优惠券队列和交换机绑定关系，并且指定 RoutingKey
     */
    @Bean
    public Binding couponBindingFanout(@Qualifier(Constant.QUEUE_PUSH_COUPON) Queue queue,
                                       @Qualifier(Constant.PUSH_DATA_FANOUT_EXCHANGE) FanoutExchange exchange) {
        return BindingBuilder.bind( queue ).to( exchange );
    }

}
